node {
    stage('git chekout') {
        git branch: "master", url: 'https://gitee.com/fastjrun/itrms.git'
    }
    stage('parallel package') {
        parallel (
                'package vue':
                {
                    sh 'cd eladmin-web && yarn install --ignore-scripts && npm run build:prod'
                },
                'package java': {
                    sh 'mvn clean package -pl eladmin-system -am -DskipTests=true -Dbdgc.skip=false'
                }
        )
    }
    stage('cp dist && jar') {
        sh 'mkdir -p output && rm -rf output/* ' +
                '&& cp -r eladmin-web/dist ./output/ ' +
                '&& cp eladmin-system/target/eladmin-system.jar ./output/eladmin-system.jar ' +
                '&& cp Dockerfile ./output && cp default.conf ./output && cp entrypoint.sh ./output'
        dir( 'output' ) {
            stash 'tarOutput'
        }
    }
    stage('parallel docker build') {
        parallel (
                'docker arm64': {
                    sh 'cd output && docker build . -t pi4k8s/itrms-arm64:1.3.0'
                    sh 'docker push pi4k8s/itrms-arm64:1.3.0'
                },
                'docker amd64': {
                    node('amd64') {
                        dir( 'output' ) {
                            unstash 'tarOutput'
                        }
                        sh 'cd output && docker build . -t pi4k8s/itrms-amd64:1.3.0'
                        sh 'docker push pi4k8s/itrms-amd64:1.3.0'
                    }
                }
        )
    }
    stage('manifest'){
        try {
            sh 'docker manifest rm pi4k8s/itrms:1.3.0'
        }catch(exc){
            echo "some thing wrong"
        }
        sh 'docker manifest create pi4k8s/itrms:1.3.0 pi4k8s/itrms-amd64:1.3.0 pi4k8s/itrms-arm64:1.3.0'
        sh 'docker manifest annotate pi4k8s/itrms:1.3.0 pi4k8s/itrms-amd64:1.3.0 --os linux --arch amd64'
        sh 'docker manifest annotate pi4k8s/itrms:1.3.0 pi4k8s/itrms-arm64:1.3.0 --os linux --arch arm64'
        sh 'docker manifest push pi4k8s/itrms:1.3.0'
    }
    stage('cleanWs'){
        parallel (
                'arm64': {
                    cleanWs()
                },
                'amd64': {
                    node('amd64') {
                        cleanWs()
                    }
                }
        )
    }
}
